<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="openstack,">










<meta name="description" content="在查看openstack的虚机时遇到了一个奇怪的问题：通过nova查询虚机的详情时，发现有的虚机能够看到IP信息，有的虚机看不到IP信息，如下：  但是奇怪的是通过dashboard是可以看到虚机的IP的，一开始不知道为什么，后来通过排查，猜想可能是虚机实例的项目和网络port的项目不一致。验证如下：">
<meta name="keywords" content="openstack">
<meta property="og:type" content="article">
<meta property="og:title" content="修复项目不一致导致的查询不到虚机IP信息问题">
<meta property="og:url" content="http://vosamo.gitee.io/blog/2019/08/09/修复项目不一致导致的查询不到虚机IP信息问题/index.html">
<meta property="og:site_name" content="VOSAMO">
<meta property="og:description" content="在查看openstack的虚机时遇到了一个奇怪的问题：通过nova查询虚机的详情时，发现有的虚机能够看到IP信息，有的虚机看不到IP信息，如下：  但是奇怪的是通过dashboard是可以看到虚机的IP的，一开始不知道为什么，后来通过排查，猜想可能是虚机实例的项目和网络port的项目不一致。验证如下：">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://vosamo.gitee.io/blog/2019/08/09/修复项目不一致导致的查询不到虚机IP信息问题/vm_list.jpg">
<meta property="og:updated_time" content="2019-08-09T09:34:33.511Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="修复项目不一致导致的查询不到虚机IP信息问题">
<meta name="twitter:description" content="在查看openstack的虚机时遇到了一个奇怪的问题：通过nova查询虚机的详情时，发现有的虚机能够看到IP信息，有的虚机看不到IP信息，如下：  但是奇怪的是通过dashboard是可以看到虚机的IP的，一开始不知道为什么，后来通过排查，猜想可能是虚机实例的项目和网络port的项目不一致。验证如下：">
<meta name="twitter:image" content="http://vosamo.gitee.io/blog/2019/08/09/修复项目不一致导致的查询不到虚机IP信息问题/vm_list.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://vosamo.gitee.io/blog/2019/08/09/修复项目不一致导致的查询不到虚机IP信息问题/">





  <title>修复项目不一致导致的查询不到虚机IP信息问题 | VOSAMO</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">VOSAMO</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://vosamo.gitee.io/blog/blog/2019/08/09/修复项目不一致导致的查询不到虚机IP信息问题/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="vosamo">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="VOSAMO">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">修复项目不一致导致的查询不到虚机IP信息问题</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-09T15:49:16+08:00">
                2019-08-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/笔记/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>在查看openstack的虚机时遇到了一个奇怪的问题：通过nova查询虚机的详情时，发现有的虚机能够看到IP信息，有的虚机看不到IP信息，如下：</p>
<img src="/blog/2019/08/09/修复项目不一致导致的查询不到虚机IP信息问题/vm_list.jpg">
<p>但是奇怪的是通过dashboard是可以看到虚机的IP的，一开始不知道为什么，后来通过排查，猜想可能是虚机实例的项目和网络port的项目不一致。验证如下：</p>
<a id="more"></a>
<p>第一步，先验证虚机的项目id和port的项目id是不是不一致</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">[root@ctl01 ~]# neutron port-show cdd446ea-fd92-4b9f-84bb-ac7ba46831b2</span><br><span class="line">+-----------------------+--------------------------------------------------------------------------------------+</span><br><span class="line">| Field                 | Value                                                                                |</span><br><span class="line">+-----------------------+--------------------------------------------------------------------------------------+</span><br><span class="line">| admin_state_up        | True                                                                                 |</span><br><span class="line">| allowed_address_pairs |                                                                                      |</span><br><span class="line">| binding:host_id       | ctl01                                                                                |</span><br><span class="line">| binding:profile       | &#123;&#125;                                                                                   |</span><br><span class="line">| binding:vif_details   | &#123;"port_filter": true, "ovs_hybrid_plug": false&#125;                                      |</span><br><span class="line">| binding:vif_type      | ovs                                                                                  |</span><br><span class="line">| binding:vnic_type     | normal                                                                               |</span><br><span class="line">| created_at            | 2019-07-05T06:05:07                                                                  |</span><br><span class="line">| description           |                                                                                      |</span><br><span class="line">| device_id             | 126d2e80-b271-402e-8837-11dd31a3671d                                                 |</span><br><span class="line">| device_owner          | compute:我是测试计算集群                                                             |</span><br><span class="line">| extra_dhcp_opts       |                                                                                      |</span><br><span class="line">| fixed_ips             | &#123;"subnet_id": "43ff8726-8b14-4a2f-b002-c2d044666f2b", "ip_address": "172.23.96.105"&#125; |</span><br><span class="line">| id                    | cdd446ea-fd92-4b9f-84bb-ac7ba46831b2                                                 |</span><br><span class="line">| mac_address           | fa:16:3e:76:b1:ea                                                                    |</span><br><span class="line">| name                  |                                                                                      |</span><br><span class="line">| network_id            | 6814645c-c390-45a3-b2db-db2937ff1170                                                 |</span><br><span class="line">| port_security_enabled | True                                                                                 |</span><br><span class="line">| security_groups       | 4e8acb6c-ba22-485a-86c5-cd25c480c5dd                                                 |</span><br><span class="line">| status                | ACTIVE                                                                               |</span><br><span class="line">| tenant_id             | 8add53107ea247eaaf828c5b0d6bf67b                                                     |</span><br><span class="line">| updated_at            | 2019-07-09T03:33:15                                                                  |</span><br><span class="line">+-----------------------+--------------------------------------------------------------------------------------+</span><br><span class="line">[root@ctl01 ~]# nova show 126d2e80-b271-402e-8837-11dd31a3671d</span><br><span class="line">+--------------------------------------+----------------------------------------------------------------------------------+</span><br><span class="line">| Property                             | Value                                                                            |</span><br><span class="line">+--------------------------------------+----------------------------------------------------------------------------------+</span><br><span class="line">| OS-DCF:diskConfig                    | AUTO                                                                             |</span><br><span class="line">| OS-EXT-AZ:availability_zone          | nova                                                                             |</span><br><span class="line">| OS-EXT-SRV-ATTR:host                 | ctl01                                                                            |</span><br><span class="line">| OS-EXT-SRV-ATTR:hostname             | group3-test                                                                      |</span><br><span class="line">| OS-EXT-SRV-ATTR:hypervisor_hostname  | ctl01                                                                            |</span><br><span class="line">| OS-EXT-SRV-ATTR:instance_name        | instance-00000377                                                                |</span><br><span class="line">| OS-EXT-SRV-ATTR:kernel_id            |                                                                                  |</span><br><span class="line">| OS-EXT-SRV-ATTR:launch_index         | 0                                                                                |</span><br><span class="line">| OS-EXT-SRV-ATTR:ramdisk_id           |                                                                                  |</span><br><span class="line">| OS-EXT-SRV-ATTR:reservation_id       | r-4bko0f25                                                                       |</span><br><span class="line">| OS-EXT-SRV-ATTR:root_device_name     | /dev/vda                                                                         |</span><br><span class="line">| OS-EXT-SRV-ATTR:user_data            | -                                                                                |</span><br><span class="line">| OS-EXT-STS:power_state               | 1                                                                                |</span><br><span class="line">| OS-EXT-STS:task_state                | -                                                                                |</span><br><span class="line">| OS-EXT-STS:vm_state                  | active                                                                           |</span><br><span class="line">| OS-SRV-USG:launched_at               | 2019-07-05T06:05:13.000000                                                       |</span><br><span class="line">| OS-SRV-USG:terminated_at             | -                                                                                |</span><br><span class="line">| accessIPv4                           |                                                                                  |</span><br><span class="line">| accessIPv6                           |                                                                                  |</span><br><span class="line">| config_drive                         |                                                                                  |</span><br><span class="line">| created                              | 2019-07-05T06:05:05Z                                                             |</span><br><span class="line">| description                          | group3_test                                                                      |</span><br><span class="line">| flavor                               | 8_16384_50_0_0 (9b74c2f4-563f-4baa-a1be-cad101cebb47)                            |</span><br><span class="line">| hostId                               | dcac0ce1818edcf236f15a8acc627885896d7a758fa369d440756a78                         |</span><br><span class="line">| host_status                          | UP                                                                               |</span><br><span class="line">| id                                   | 126d2e80-b271-402e-8837-11dd31a3671d                                             |</span><br><span class="line">| image                                | 2019051717-centos75x64 (e45e9e1e-6bbb-4be6-b2ac-93e9d4e945a8)                    |</span><br><span class="line">| key_name                             | -                                                                                |</span><br><span class="line">| locked                               | False                                                                            |</span><br><span class="line">| metadata                             | &#123;&#125;                                                                               |</span><br><span class="line">| name                                 | group3_test                                                                      |</span><br><span class="line">| os-extended-volumes:volumes_attached | [&#123;"id": "95dbe34f-d022-4b9e-a2aa-04669a9fce89", "delete_on_termination": false&#125;] |</span><br><span class="line">| progress                             | 0                                                                                |</span><br><span class="line">| security_groups                      | default                                                                          |</span><br><span class="line">| status                               | ACTIVE                                                                           |</span><br><span class="line">| tenant_id                            | 7540e750f9e54fcbb8fc6e97ecbc17c1                                                 |</span><br><span class="line">| updated                              | 2019-07-05T06:05:13Z                                                             |</span><br><span class="line">| user_id                              | 98deedecc83e48cca6b656e609a9c4b6                                                 |</span><br><span class="line">+--------------------------------------+----------------------------------------------------------------------------------+</span><br></pre></td></tr></table></figure>
<p>可以看出二者的tanant_id（项目id）确实不一致。</p>
<p>第二步，对该猜测进行验证</p>
<p>找一台正常的虚机，故意将虚机的port的项目改成和实例不一致（需要修改neutron库的ports表），然后过一会儿去查看虚机的详情，果然看不到IP信息了。到这里我喜出望外，只要将port的项目改成和虚机实例一致不就解决了？于是我将port的项目id改成和虚机一样，再去查看虚机的详情，然而，并没有如我所料的那样可以看到虚机的IP信息。这是怎么回事呢？</p>
<p>后来发现，虚机IP信息获取不到，跟nova库的instance_info_caches表的network_info字段有关，如果这个字段为空就查询不到IP信息，正常情况下这个字段是一个很长的json串，如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">[&#123;</span><br><span class="line">	<span class="attr">"profile"</span>: &#123;&#125;,</span><br><span class="line">	<span class="attr">"ovs_interfaceid"</span>: <span class="string">"d07fb21b-3b83-4e45-a4c6-d7e27181f499"</span>,</span><br><span class="line">	<span class="attr">"preserve_on_delete"</span>: <span class="literal">false</span>,</span><br><span class="line">	<span class="attr">"network"</span>: &#123;</span><br><span class="line">		<span class="attr">"bridge"</span>: <span class="string">"br-int"</span>,</span><br><span class="line">		<span class="attr">"subnets"</span>: [&#123;</span><br><span class="line">			<span class="attr">"ips"</span>: [&#123;</span><br><span class="line">				<span class="attr">"meta"</span>: &#123;&#125;,</span><br><span class="line">				<span class="attr">"version"</span>: <span class="number">4</span>,</span><br><span class="line">				<span class="attr">"type"</span>: <span class="string">"fixed"</span>,</span><br><span class="line">				<span class="attr">"floating_ips"</span>: [],</span><br><span class="line">				<span class="attr">"address"</span>: <span class="string">"172.23.97.2"</span></span><br><span class="line">			&#125;],</span><br><span class="line">			<span class="attr">"version"</span>: <span class="number">4</span>,</span><br><span class="line">			<span class="attr">"meta"</span>: &#123;</span><br><span class="line">				<span class="attr">"dhcp_server"</span>: <span class="string">"172.23.97.1"</span></span><br><span class="line">			&#125;,</span><br><span class="line">			<span class="attr">"dns"</span>: [],</span><br><span class="line">			<span class="attr">"routes"</span>: [],</span><br><span class="line">			<span class="attr">"cidr"</span>: <span class="string">"172.23.97.0/24"</span>,</span><br><span class="line">			<span class="attr">"gateway"</span>: &#123;</span><br><span class="line">				<span class="attr">"meta"</span>: &#123;&#125;,</span><br><span class="line">				<span class="attr">"version"</span>: <span class="number">4</span>,</span><br><span class="line">				<span class="attr">"type"</span>: <span class="string">"gateway"</span>,</span><br><span class="line">				<span class="attr">"address"</span>: <span class="string">"172.23.97.254"</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;],</span><br><span class="line">		<span class="attr">"meta"</span>: &#123;</span><br><span class="line">			<span class="attr">"injected"</span>: <span class="literal">false</span>,</span><br><span class="line">			<span class="attr">"tenant_id"</span>: <span class="string">"ec76d161527e4eb7be3746802b745a40"</span>,</span><br><span class="line">			<span class="attr">"mtu"</span>: <span class="number">1500</span></span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="attr">"id"</span>: <span class="string">"bfc697f1-37ee-4ad5-8434-8eea42e45838"</span>,</span><br><span class="line">		<span class="attr">"label"</span>: <span class="string">"vlan2597"</span></span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="attr">"devname"</span>: <span class="string">"tapd07fb21b-3b"</span>,</span><br><span class="line">	<span class="attr">"vnic_type"</span>: <span class="string">"normal"</span>,</span><br><span class="line">	<span class="attr">"qbh_params"</span>: <span class="literal">null</span>,</span><br><span class="line">	<span class="attr">"meta"</span>: &#123;&#125;,</span><br><span class="line">	<span class="attr">"details"</span>: &#123;</span><br><span class="line">		<span class="attr">"port_filter"</span>: <span class="literal">true</span>,</span><br><span class="line">		<span class="attr">"ovs_hybrid_plug"</span>: <span class="literal">false</span></span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="attr">"address"</span>: <span class="string">"fa:16:3e:14:94:b8"</span>,</span><br><span class="line">	<span class="attr">"active"</span>: <span class="literal">false</span>,</span><br><span class="line">	<span class="attr">"type"</span>: <span class="string">"ovs"</span>,</span><br><span class="line">	<span class="attr">"id"</span>: <span class="string">"d07fb21b-3b83-4e45-a4c6-d7e27181f499"</span>,</span><br><span class="line">	<span class="attr">"qbg_params"</span>: <span class="literal">null</span></span><br><span class="line">&#125;]</span><br></pre></td></tr></table></figure>
<p>这是一个list，如果虚机有多个网卡（port），那这个list就有多个元素。显然从名字上就可以看出instance_info_caches表应该是虚机的信息缓存表，我觉得openstack会刷新此表，但是什么时候触发刷新就不知道了。为什么将项目改为一致没有触发刷新呢？也不清楚。</p>
<p>为了解决查询不到IP信息的问题，我想出了一个治标不治本的笨办法：只要我按照上面的格式还原出虚机的network_info来不就行了？</p>
<p>我看了一下这个json结构，里面的每个元素是一个dict，dict里面大多数的数据是相同的，只有部分个性化的元素如port_id,network_id,network_name,mac_address等信息不相同。我只需要查询出这些信息来进行替换就可以了。</p>
<p>于是我写了下面这个脚本：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/python</span></span><br><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"><span class="keyword">import</span> MySQLdb</span><br><span class="line"><span class="comment"># 查询构造network_info需要字段的sql语句</span></span><br><span class="line">SQL=<span class="string">"select pt.tenant_id,pt.id as port_id, concat('tap',left(pt.id,11)) as dev_name, pt.network_id,pt.mac_address,pt.device_id, \</span></span><br><span class="line"><span class="string">ipl.ip_address,ipl.subnet_id,subn.cidr,subn.gateway_ip,n.name network_name from neutron.ports pt, neutron.ipallocations ipl,neutron.subnets subn, \</span></span><br><span class="line"><span class="string">neutron.networks n where pt.id=ipl.port_id and subn.id=ipl.subnet_id and ipl.network_id=n.id \</span></span><br><span class="line"><span class="string">and device_id IN (SELECT instance_uuid FROM nova.instance_info_caches WHERE network_info = '[]' AND deleted_at IS NULL);"</span></span><br><span class="line"><span class="comment"># 查询网络dhcp地址的sql语句</span></span><br><span class="line">BASE_DHCP_SQL=<span class="string">"select ip_address as dhcp_addr from neutron.ipallocations ipl where port_id in (select id from neutron.ports where device_owner like '%%network:dhcp%%' and network_id='%(network_id)s') and subnet_id='%(subnet_id)s';"</span></span><br><span class="line"><span class="comment"># 构造network_info数据的模板，用字符串加占位符形式</span></span><br><span class="line">BASE_NETWORK_INFO=<span class="string">'&#123;"profile":&#123;&#125;,"ovs_interfaceid":"%(port_id)s","preserve_on_delete":false,"network":&#123;"bridge":"br-int","subnets":[&#123;"ips":[&#123;"meta":&#123;&#125;,"version":4,"type":"fixed","floating_ips":[],"address":"%(ip_address)s"&#125;],"version":4,"meta":&#123;"dhcp_server":"%(dhcp_addr)s"&#125;,"dns":[&#123;"meta":&#123;&#125;,"version":4,"type":"dns","address":"114.114.114.114"&#125;],"routes":[],"cidr":"%(cidr)s","gateway":&#123;"meta":&#123;&#125;,"version":4,"type":"gateway","address":"%(gateway_ip)s"&#125;&#125;],"meta":&#123;"injected":false,"tenant_id":"%(tenant_id)s","mtu":1500&#125;,"id":"%(network_id)s","label":"%(network_name)s"&#125;,"devname":"%(dev_name)s","vnic_type":"normal","qbh_params":null,"meta":&#123;&#125;,"details":&#123;"port_filter":true,"ovs_hybrid_plug":true&#125;,"address":"%(mac_address)s","active":true,"type":"ovs","id":"%(port_id)s","qbg_params":null&#125;'</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存虚机和网络信息的映射关系</span></span><br><span class="line">VM_NET_MAP=&#123;&#125;</span><br><span class="line">count=<span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 连接数据库，按照你自己的情况修改用户名和密码</span></span><br><span class="line">conn = MySQLdb.connect(host=<span class="string">"localhost"</span>,user=<span class="string">"root"</span>,passwd=<span class="string">"xxxx"</span>,db=<span class="string">"nova"</span>,charset=<span class="string">"utf8"</span>)</span><br><span class="line">cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)</span><br><span class="line">cur.execute(SQL)</span><br><span class="line">lines = cur.fetchall()</span><br><span class="line"><span class="comment">#print lines</span></span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> lines:</span><br><span class="line">    device_id=line[<span class="string">"device_id"</span>]</span><br><span class="line">    count=count+<span class="number">1</span></span><br><span class="line">    <span class="comment">#print line["network_id"]</span></span><br><span class="line">    <span class="comment">#print line["mac_address"]]</span></span><br><span class="line">    <span class="keyword">print</span> line</span><br><span class="line">    <span class="keyword">print</span> <span class="string">"**********************************************"</span></span><br><span class="line">    DHCP_SQL=BASE_DHCP_SQL % line</span><br><span class="line">    cur.execute(DHCP_SQL)</span><br><span class="line">    dhcp_line=cur.fetchall()</span><br><span class="line">    <span class="comment"># 查询dhcp地址</span></span><br><span class="line">    <span class="keyword">if</span> len(dhcp_line)&gt;<span class="number">0</span>:</span><br><span class="line">        line[<span class="string">"dhcp_addr"</span>]=dhcp_line[<span class="number">0</span>][<span class="string">"dhcp_addr"</span>]</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">	    line[<span class="string">"dhcp_addr"</span>]=<span class="string">""</span>;</span><br><span class="line">    <span class="comment"># 构造network_info</span></span><br><span class="line">    NETWORK_INFO=BASE_NETWORK_INFO % line</span><br><span class="line">    <span class="keyword">print</span> NETWORK_INFO</span><br><span class="line">    <span class="comment"># 以虚机id为key构造map</span></span><br><span class="line">    <span class="keyword">if</span> VM_NET_MAP.has_key(device_id):</span><br><span class="line">        VM_NET_MAP[device_id].append(NETWORK_INFO)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        VM_NET_MAP[device_id]=[NETWORK_INFO]</span><br><span class="line">    <span class="keyword">print</span> <span class="string">"=============================================="</span></span><br><span class="line"><span class="keyword">print</span> <span class="string">"count: %d"</span> % count</span><br><span class="line"><span class="keyword">print</span> <span class="string">"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"</span></span><br><span class="line"><span class="keyword">print</span> VM_NET_MAP</span><br><span class="line"><span class="keyword">print</span> <span class="string">"++++++++++++++++++++++++++++\n"</span>*<span class="number">5</span></span><br><span class="line"><span class="keyword">for</span> (k,v) <span class="keyword">in</span> VM_NET_MAP.items():</span><br><span class="line">    <span class="keyword">print</span> k + <span class="string">"\t"</span></span><br><span class="line">    <span class="keyword">print</span> v</span><br></pre></td></tr></table></figure>
<p>脚本最后按照key-value打印了network_info信息，整体上没有太大问题，但是由于构造的时候是用字符串加占位符拼接组装的，所以还是一个字符串，需要将最后打印的key-value信息拷贝到notepad等文本编辑器中手工处理一下：</p>
<ul>
<li>将<code>u&#39;{</code>替换为<code>{</code></li>
<li>将<code>}&#39;</code>替换为<code>}</code></li>
</ul>
<p>然后借助notepad强大的列编辑功能，构造sql语句<code>update nova.instance_info_caches set network_info=&#39;xxxx&#39; where instance_uuid=&#39;xxxx&#39;;</code>，在nova数据库中执行一下。再去查看虚机详细信息，发现能够获取IP信息了。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/openstack/" rel="tag"># openstack</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/07/31/spring学习-Bean与元数据配置/" rel="next" title="spring学习-Bean与元数据配置">
                <i class="fa fa-chevron-left"></i> spring学习-Bean与元数据配置
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/avatar.gif" alt="vosamo">
            
              <p class="site-author-name" itemprop="name">vosamo</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vosamo</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
